﻿using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Interfaces;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Types;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.WorksheetElements
{
    [XmlRoot("c", Namespace = Constants.ExcelMain)]
    public class Cell : XmlSerializable, ICopyable
    {
        #region Attributes

        [Optional]
        [XmlAttribute("r", Form = XmlSchemaForm.Qualified)]
        public string Reference { get; set; }

        [Optional]
        [XmlAttribute("s", Form = XmlSchemaForm.Qualified)]
        public string StyleIndex { get; set; }

        [Optional]
        [XmlAttribute("t", Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_CellType> CellType { get; set; }

        [Optional]
        [XmlAttribute("cm", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> CellMetadataIndex { get; set; }

        [Optional]
        [XmlAttribute("vm", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> ValueMetadataIndex { get; set; }

        [Optional]
        [XmlAttribute("ph", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> ShowPhonetic { get; set; }

        #endregion

        #region Elements

        [Optional]
        [XmlElement("v", Namespace = Constants.ExcelMain)]
        public SimpleText Value { get; set; }

        [Optional]
        [XmlElement("f", Namespace = Constants.ExcelMain)]
        public Formula Formula { get; set; }

        [Optional]
        [XmlElement("extLst", Namespace = Constants.ExcelMain)]
        public ExtensionList ExtList { get; set; }

        [Optional]
        [XmlElement("is", Namespace = Constants.ExcelMain)]
        public InlineRichText InlineRichText { get; set; }

        #endregion

        [XmlIgnore]
        private static char[] abc = 
        { 
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };

        [XmlIgnore]
        public int Index
        {
            get
            {
                int index = 0;
                if (Reference != null)
                {
                    char[] array = Reference.Replace(
                        Parent.Index.ToString(), string.Empty).ToCharArray();
                    for (int i = 0; i < array.Length; ++i)
                    {
                        index += (int)Math.Pow(26, (array.Length - i - 1)) * Array.IndexOf(abc, array[i]);
                    }
                }
                return index;
            }
            set
            {
                int num = value - 1;
                string reference = string.Empty;
                while (true)
                {
                    var i = num % 26;
                    reference = abc[i] + reference;
                    num = (num - i) / 26;

                    if (num < 1)
                    {
                        break;
                    }
                }
                Reference = reference + Parent.Index.ToString();
            }
        }

        [XmlIgnore]
        internal Row Parent { get; set; }

        public Cell() { }

        public Formula AddFormula(string text)
        {
            this.Formula = new Formula() { Value = text };
            return this.Formula;
        }

        public SimpleText AddSimpleText(string text)
        {
            this.Value = new SimpleText() { Value = text };
            return this.Value;
        }

        public static string Resolve(int value)
        {
            int num = value - 1;
            string reference = string.Empty;
            while (true)
            {
                var i = num % 26;
                reference = abc[i] + reference;
                num = (num - i) / 26;

                if (num < 1)
                {
                    break;
                }
            }
            return reference;
        }

        public void Copy(object obj)
        {
            if (obj is Cell)
            {
                this.Value = (obj as Cell).Value;
                this.ExtList = (obj as Cell).ExtList;
                this.Formula = (obj as Cell).Formula;
                this.CellType = (obj as Cell).CellType;
                this.Reference = (obj as Cell).Reference;
                this.StyleIndex = (obj as Cell).StyleIndex;
                this.ShowPhonetic = (obj as Cell).ShowPhonetic;
                this.InlineRichText = (obj as Cell).InlineRichText;
                this.CellMetadataIndex = (obj as Cell).CellMetadataIndex;
                this.ValueMetadataIndex = (obj as Cell).ValueMetadataIndex;
            }
        }
    }
}
